
package com.android.launcher3;

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.Log;

/**
 * 用于桌面设置
 * 
 */
public class PreferenceWithDesktopSetting extends PreferenceActivity {

	private ListPreference mListPref;

	private ListPreference mListPrefMenuOut;

	private ListPreference mListPrefMenuIcon;

	private static final String TAG = "PreferenceWithDesktopSetting";

	public static final String KEY_SELECT_EFFECT = "WhichEffect";

	public static final String KEY_DESKTOP = "DesktopListPreference";

	public static final String KEY_FUC_OUT_MENU = "FuctionTableEffect";

	public static final String KEY_FUC_ICON_MENU = "FuctionTableIconEffect";

	private static final String KEY_SELECT = "DesktopListPreference";

	private String EFFECT[] = null;

	private String FUNCTION_OUT_MENU[] = null;

	private String FUNCTION_ICON_MENU[] = null;

	/**
	 * 默认第一个动画
	 */
	private String mSelectEffect;

	private String mMenuOutEffect;

	private String mMenuIconEffect;

	public interface IExecuteEffect {
		public void execute();
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.desktop_setting);

		init();
	}

	private void init() {

		mListPref = (ListPreference) findPreference(KEY_SELECT);

		mListPrefMenuOut = (ListPreference) findPreference(KEY_FUC_OUT_MENU);

		mListPrefMenuIcon = (ListPreference) findPreference(KEY_FUC_ICON_MENU);
		

		mListPref.setOnPreferenceChangeListener(changeListener);
		
		mListPrefMenuOut.setOnPreferenceChangeListener(changeListener);
		
		mListPrefMenuIcon.setOnPreferenceChangeListener(changeListener);

		EFFECT = getResources().getStringArray(R.array.entries_list_preference);

		FUNCTION_OUT_MENU = getResources().getStringArray(
				R.array.entries_list_fuct_out_menu);

		FUNCTION_ICON_MENU = getResources().getStringArray(
				R.array.entries_list_fuct_icon_menu);

		// /获取用户保存的动画值
		mSelectEffect = mListPref.getValue();

		// 默认第一个动画
		if (mSelectEffect == null) {

			mSelectEffect = getResources().getStringArray(
					R.array.entriesvalue_list_preference)[0];
		}

		mListPref.setSummary(EFFECT[Integer.parseInt(mSelectEffect)]);

		mListPref.setValue(mSelectEffect);

		// /////////////////

		mMenuOutEffect = mListPrefMenuOut.getValue();

		if (mMenuOutEffect == null) {
			mMenuOutEffect = getResources().getStringArray(
					R.array.entriesvalue_list_fuct_out_menu)[0];
		}
		
		mListPrefMenuOut.setSummary(FUNCTION_OUT_MENU[Integer.parseInt(mMenuOutEffect)]);

		mListPrefMenuOut.setValue(mMenuOutEffect);
		
		
		this.mMenuIconEffect = this.mListPrefMenuIcon.getValue();

		if (mMenuIconEffect == null) {
			mMenuIconEffect = getResources().getStringArray(
					R.array.entriesvalue_list_fuct_icon_menu)[0];
		}
		
		mListPrefMenuIcon.setSummary(FUNCTION_ICON_MENU[Integer.parseInt(mMenuIconEffect)]);

		mListPrefMenuIcon.setValue(mMenuIconEffect);
		
		

	}

	private Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() {
		public boolean onPreferenceChange(Preference preference, Object newValue) {

			String key = preference.getKey();

			Log.d(TAG, "key:" + key);

			// /动画特效对应的值
			Integer select = Integer.parseInt(newValue.toString());
			if (key.equals(KEY_DESKTOP)) {

				if (EFFECT != null) {
					preference.setSummary(EFFECT[select]);
				}

				Workspace.setAninmationType(select);
			} else if (key.equals(KEY_FUC_OUT_MENU)) {

				if (FUNCTION_OUT_MENU != null) {
					preference.setSummary(FUNCTION_OUT_MENU[select]);
				}
				
				MenuManager.setAninmationType(select);
			} 
			else 
				if (key.equals(KEY_FUC_ICON_MENU)) {
					if (FUNCTION_ICON_MENU != null) {
						preference.setSummary(FUNCTION_ICON_MENU[select]);
					}
					AppMenu.setAninmationType(select);
				}

			return true;
		}

	};

}
